package com.zhengtoon.mctoon.conv.utils;


import com.alibaba.fastjson.JSON;
import com.bjtoon.cdpf.sdk.util.ParamCodeUtils;
import com.bjtoon.framework.sdk.toon.tooncode.ToonCodeInfo;
import com.bjtoon.framework.sdk.toon.tooncode.ToonCodeUtils;
import com.bjtoon.framework.sdk.toon.tooncode.ToonDesUtils;
import com.qitoon.framework.core.exception.BusinessException;
import com.zhengtoon.mctoon.conv.bean.toonCode.VisitorInfo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;

/**
 * toonCode 相关工具类.
 *
 * @author liuzhenhua
 * @since 2017-05-26
 */
public class ToonCodeAppUtils {

    private final static Logger log = LoggerFactory.getLogger(ToonCodeAppUtils.class);

    // toon code 的过期时间(分钟)
    private final static String TOON_CODE_EXPIRED_MINUTES = SysConfig.getPropertyValue("config.toon_code_expired_minutes");

    // toon code 的过期时间(分钟)
    private final static String APP_SECRET = SysConfig.getPropertyValue("config.appSecret");

    // 一分钟毫秒数
    private final static long MILL_SECONDS = 60 * 1000;

    private final static Long CODE_EXPIRED_SECONDS = Long.valueOf(TOON_CODE_EXPIRED_MINUTES) * MILL_SECONDS;


    public static String encryptCode(ToonCodeInfo codeInfo) {

        String encryptText = ToonDesUtils.encryptWithSecret(JSON.toJSONString(codeInfo), APP_SECRET);

        return encryptText;
    }

    /**
     * 解析toonCode   144637
     *
     * @param toonCode ToonCodeInfo
     */
    public static ToonCodeInfo decryptToonCode(String toonCode) {
        try {
            String code = URLEncoder.encode(toonCode);  // POST方式需要重新encode
            return ToonCodeUtils.decryptToonCode(code, APP_SECRET);
        } catch (Exception e) {
            log.error("toonCode解析失败", e);
            return null;
        }
    }


    /**
     * 解析toon基础 toonCode   144637
     *
     * @param toonCode ToonCodeInfo
     */
    public static VisitorInfo decryptBaseToonCode(String toonCode) {

        String code = URLEncoder.encode(toonCode);  // POST方式需要重新encode

        String decryptText = ToonDesUtils.decrypt(code, APP_SECRET);

        VisitorInfo visitorInfo = JSON.parseObject(decryptText,
                VisitorInfo.class);

        return visitorInfo;
    }

    /**
     * 解析cityCode
     *
     * @param cityCode ToonCodeInfo
     */
    public static String decryptCityCode(String cityCode) {
        String code = URLEncoder.encode(cityCode);  // POST方式需要重新encode
        return ParamCodeUtils.resolveCodeForStr(code, APP_SECRET);
    }


    /**
     * 判断时间戳是否过期    144637
     *
     * @param timestamp 时间戳
     * @return boolean
     */
    public static boolean isExpired(long timestamp) {
        long currentTime = System.currentTimeMillis();
        return currentTime - timestamp >= CODE_EXPIRED_SECONDS;
    }

    /**
     * 判断code是否过期   144637
     *
     * @param toonCode toon code
     * @return boolean
     */
    public static boolean isCodeExpired(String toonCode) {
        ToonCodeInfo codeInfo = null;
        try {
            codeInfo = decryptToonCode(toonCode);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BusinessException("", "解析code失败");
        }
        if (null == codeInfo) {
            return false;
        }
        Long timestamp = codeInfo.getTimestamp();
        return isExpired(timestamp);
    }

    /**
     * 判断code是否过期   144637
     *
     * @param codeInfo codeInfo
     * @return boolean
     */
    public static boolean isCodeExpired(ToonCodeInfo codeInfo) {
        if (null == codeInfo) {
            return true;
        }
        // 如果为通知类型, 不会过期
        if (AppConstant.TOON_CODE_TYPE_MSG.equals(codeInfo.getToonType())) {
            return false;
        }
        Long timestamp = codeInfo.getTimestamp();
        return isExpired(timestamp);
    }

}
